<?php

namespace App\Console\Commands;

use App\Http\Controllers\Api\MiniProgramController;
use App\Models\Order;
use GuzzleHttp\Client;
use Illuminate\Console\Command;
use Endroid\QrCode\Response\QrCodeResponse;

class CreateMpCode extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'create:mpcode';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '生成订单二维码';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //查询支付成功的订单，生成订单小程序码
        $orders = Order::join('products','orders.product_id','=','products.id')->select('products.hexiao_type','orders.*')
                        ->where('orders.status',TO_DELIVERY)->where('hexiao_type',DAODAIN_HEXIAO)
                        ->whereNull('mp_code')->OrderBy('orders.id','asc')->limit(1)->get();
        if (count($orders) > 0) {
            $mn_program = new MiniProgramController();
            foreach ($orders as $order) {
                $order->mp_code = $mn_program->getUnlimited('order_id',$order->id, 'pages/order/hexiao');
                $order->save();
            }
        }
    }
}
